﻿using System;
using System.Data;
using System.Data.Common;
using XianShang.Common;
using XianShang.Entity;
namespace XianShang.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        /// <summary>
        /// 增加一條信息（來源為空時）
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddNoticeInfo(NoticeInfo Ninfo)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@Title",(DbType)SqlDbType.NVarChar,300,Ninfo.Title),
                                       DbHelper.MakeInParam("@Content",(DbType)SqlDbType.NText,1073741823,Ninfo.Content),
                                       DbHelper.MakeInParam("@Times",(DbType)SqlDbType.DateTime,DateTime.Now.ToString().Length,Ninfo.Times),
                                       DbHelper.MakeInParam("@User",(DbType)SqlDbType.NVarChar,200,Ninfo.User),
                                       DbHelper.MakeInParam("@IsShow",(DbType)SqlDbType.Bit,5,Ninfo.IsShow)
                                   };
            string commandText = "INSERT INTO [dbo].[XS_Notice]([User],[Title],[Content],[IsShow],[Time])VALUES(@User,@Title,@Content,@IsShow,@Times)";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }



        /// <summary>
        /// 修改一條公告信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int UpNoticeInfo(NoticeInfo Ninfo)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@id",(DbType)SqlDbType.Int,int.MaxValue,Ninfo.ID),
                                        DbHelper.MakeInParam("@Title",(DbType)SqlDbType.NVarChar,300,Ninfo.Title),
                                       DbHelper.MakeInParam("@Content",(DbType)SqlDbType.NText,1073741823,Ninfo.Content),
                                       DbHelper.MakeInParam("@Times",(DbType)SqlDbType.DateTime,DateTime.Now.ToString().Length,Ninfo.Times),
                                       DbHelper.MakeInParam("@IsShow",(DbType)SqlDbType.Bit,5,Ninfo.IsShow)
                                   };
            string commandText = "UPDATE [dbo].[XS_Notice] SET [Title]=@Title,[Content]=@Content,[IsShow]=@isshow WHERE [ID]=@Id";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }

        /// <summary>
        /// 獲取信息顯示公告列表
        /// </summary>
        /// <returns></returns>
        public IDataReader GetNoticeInfo()
        {
            string commandText = "SELECT * FROM [dbo].[XS_Notice] order by [Time] desc";

            return DbHelper.ExecuteReader(CommandType.Text, commandText);
        }
        /// <summary>
        /// 獲取信息顯示公告列表
        /// </summary>
        /// <param name="isShow">顯示狀態（1顯示0不顯示）</param>
        /// <returns></returns>
        public IDataReader GetNoticeInfo(int isShow)
        {
            DbParameter getPar = DbHelper.MakeInParam("@isshow", (DbType)SqlDbType.Int, int.MaxValue, isShow);
            string commandText = "SELECT * FROM [dbo].[XS_Notice] WHERE [ISSHOW]=@isshow order by [Time] desc";

            return DbHelper.ExecuteReader(CommandType.Text, commandText,getPar);
        }

        /// <summary>
        /// 獲取一條要修改的公告信息顯示
        /// </summary>
        /// <returns></returns>
        public DataSet GetNoticeId(string Id)
        {
            DbParameter getPar = DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int, int.MaxValue, Id);
            string commandText = "SELECT * FROM [dbo].[XS_Notice] WHERE [Id]=@Id";
            return DbHelper.ExecuteDataset(CommandType.Text, commandText, getPar);
        }

        /// <summary>
        /// 根據ID刪除公告信息
        /// </summary>
        /// <returns></returns>
        public int DelNoticeInfo(string Id)
        {
            DbParameter getPar = DbHelper.MakeInParam("@id", (DbType)SqlDbType.Int, int.MaxValue, Id);
            string commandText = "DELETE FROM [dbo].[XS_Notice] WHERE [Id]=@Id";

            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar);
        }

    }
}
